<template>
  <div>
    <QuillEditor
      ref="myQuillEditor"
      theme="snow"
      v-model:content="content"
      :options="data.editorOption"
      contentType="html"
      @update:content="setValue()"
    />
    <!-- 使用自定义图片上传 -->
    <input
      type="file"
      hidden
      accept=".jpg,.png"
      ref="fileBtn"
      @change="handleUpload"
    />
  </div>
</template>

<script setup>
import { QuillEditor } from "@vueup/vue-quill";
import "@vueup/vue-quill/dist/vue-quill.snow.css";
import { reactive, onMounted, ref, toRaw, watch } from "vue";

const props = defineProps(["value"]);
const emit = defineEmits(["updateValue"]);
const content = ref("");
const myQuillEditor = ref();

const fileBtn = ref();
const data = ref({
  content: "",
  editorOption: {
    modules: {
      toolbar: [
        ["bold", "italic", "underline", "strike"],
        [{ size: ["small", false, "large", "huge"] }],
        [{ font: [] }],
        [{ align: [] }],
        [{ indent: "-1" }, { indent: "+1" }],
        [{ header: 1 }, { header: 2 }],
        [{ color: [] }, { background: [] }],
      ],
    },
    placeholder: "请输入内容...",
    readOnly: false,
  },
});
const imgHandler = (state) => {
  if (state) {
    fileBtn.value.click();
  }
};
const setValue = () => {
  const text = toRaw(myQuillEditor.value).getHTML();
  emit("updateValue", text);
};
const handleUpload = (e) => {
  const files = Array.prototype.slice.call(e.target.files);
  // console.log(files, "files")
  if (!files) {
    return;
  }
  const formdata = new FormData();
  formdata.append("file", files[0]);
};
onMounted(() => {
  const quill = toRaw(myQuillEditor.value).getQuill();

  if (myQuillEditor.value) {
    quill.getModule("toolbar").addHandler("image", imgHandler);
  }
  toRaw(myQuillEditor.value).setHTML(props.value);
});

//深度监听props.value的变化
watch(
  () => props.value,
  (newVal) => {
    if (newVal !== content.value) {
      toRaw(myQuillEditor.value).setHTML(props.value);
    }
  }
);
</script>
<style scoped>
:deep(.ql-editor) {
  min-height: 180px;
}
:deep(.ql-formats) {
  height: 21px;
  line-height: 21px;
}
</style>
